HackerRank Highest Value Palindrome
https://www.hackerrank.com/challenges/richie-rich/problem
提出
22/33 test cases failed :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import deque
#
# Complete the 'highestValuePalindrome' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER n
# 3. INTEGER k limit to change
#
def highestValuePalindrome(s, n, k):
# Write your code here
trim_s = s.replace(' ', '')
diff_idx = deque([])
for i in range((len(trim_s) // 2)):
if (trim_si != trim_slen(trim_s) - i - 1):
diff_idx.append(i)
if (len(diff_idx) > k):
return "-1"
list_s = list(trim_s)
for i in diff_idx:
if (k < 0):
break
if (list_si == "9" and list_slen(trim_s) - i - 1 == "9"):
continue
elif (list_si == "9"):
k -= 1
list_slen(trim_s) - i - 1 = "9"
elif (list_slen(trim_s) - i - 1 == "9"):
k -= 1
list_si = "9"
else:
k -= 2
if (k < 0):
break
else:
list_si = "9"
list_slen(trim_s) - i - 1 = "9"
return "".join(list_s)
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
k = int(first_multiple_input1)
s = input()
result = highestValuePalindrome(s, n, k)
fptr.write(result + '\n')
fptr.close()
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'highestValuePalindrome' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER n
# 3. INTEGER k
#
def highestValuePalindrome(s, n, k):
s = list(s)
changed = False * n
l = 0
r = n-1
while l <= r:
if sl != sr:
if sl > sr:
sr = sl
changedr = True
else:
sl = sr
changedl = True
k -= 1
l += 1
r -= 1
if k < 0:
return "-1"
# maximize the digits
l = 0
r = n-1
while l <= r:
# center
if l == r and k >= 1:
sl = '9'
break
if sl < '9':
# no changes before
if (not changedl and not changedr) and k >= 2:
sl = sr = '9'
k -= 2
# changed before
if (changedl or changedr) and k >= 1:
sl = sr = '9'
k -= 1
l += 1
r -= 1
return "".join(s)
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
k = int(first_multiple_input1)
s = input()
result = highestValuePalindrome(s, n, k)
fptr.write(result + '\n')
fptr.close()
提出
22/33 test cases failed :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'highestValuePalindrome' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER n
# 3. INTEGER k
#
def highestValuePalindrome(s, n, k):
# Write your code here
diff = []
for i in range(n//2):
if si != sn-1-i:
change = min(si, sn-1-i)
diff.append((i if change == si else n-1-i, max(si, sn-1-i)))
if len(diff) > k:
return "-1"
elif len(diff) == k:
res = list(s)
for i, num in diff:
resi = num
return "".join(res)
else:
nine = k - len(diff)
res = list(s)
for i, num in diff:
if nine > 0:
resi = "9"
resn-1-i = "9"
nine -= 1
else:
resi = num
return "".join(res)
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
k = int(first_multiple_input1)
s = input()
result = highestValuePalindrome(s, n, k)
fptr.write(result + '\n')
fptr.close()
提出
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'highestValuePalindrome' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER n
# 3. INTEGER k
#
# 3943 -> 39, 34 -> (9>4) -> 39, 39 -> 3994
# 092282 -> 092, 282 -> (0<2) or (9>8) -> 092, 292 -> 992, 992 -> 992299
# 12321 -> 12, 3, 12 -> 12, 9, 12 -> 12921
def highestValuePalindrome(s, n, k):
# Write your code here
if n % 2 == 0:
c = 0
front = s:n//2
back = sn//2:
for i in range(n//2):
if fronti != backi:
c += 1
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
k = int(first_multiple_input1)
s = input()
result = highestValuePalindrome(s, n, k)
fptr.write(result + '\n')
fptr.close()